<template>
  <div class="app-container">
    <el-divider>
      <div class="my-divider">基础信息</div>
    </el-divider>

    <el-form ref="form" :model="form" :rules="rules" label-width="85px" size="small"
    >
      <el-form-item label="检验单名称" prop="reportQcName">
        <el-input v-model="form.reportQcName" placeholder="请输入质检单名称"
                  class="my-input"
        />
      </el-form-item>
      <el-form-item label="检验类型" prop="reportType">
        <el-select
          v-model="form.reportType"
          placeholder="请选择检验类型"
          class="my-input"
        >
          <el-option
            v-for="dict in getQualityType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检验日期" prop="reportTime">
        <el-date-picker clearable
                        v-model="form.reportTime"
                        type="date"
                        value-format="yyyy-MM-dd hh:mm:ss"
                        placeholder="请选择质检时间"
                        class="my-input"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检验人" prop="reportQcPeo">
        <MyPopoverBtn placeholder="请选择检验人" :content="form.selectedQcPeo ? form.selectedQcPeo.userName : ''"
                      class="my-input"
                      @click="() => qcPeoDialogVisible = true"
                      :disabled="form.reportQcCode"
        />
      </el-form-item>
      <el-form-item label="备注" prop="reportDesc" class="my-desc">
        <el-input v-model="form.reportDesc" type="textarea" placeholder="请输入内容" :autosize="{minRows: 3}"
        />
      </el-form-item>

      <el-divider>
        <div class="my-divider">工单信息</div>
      </el-divider>
      <el-form-item label="工单名称" prop="reportOCodeName">
        <MyPopoverBtn placeholder="请选择工单" :content="form.reportOCodeName"
                      @click="() => workOrderDialogVisible = true"
                      :disabled="form.reportQcCode"
        />
      </el-form-item>
      <el-form-item label="工单编号" prop="reportOCode">
        <el-input v-model="form.reportOCode"
                  class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="reportPCode">
        <el-input :value="form.selectedGood ? form.selectedGood.pdName : ''"
                  class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="reportPCode">
        <el-input :value="form.selectedGood ? form.selectedGood.pdCode : ''"
                  class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="reportPCode">
        <el-input :value="form.selectedGood ? form.selectedGood.pdSpecType : ''"
                  class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="产品类型" prop="reportPCode">
        <el-input
          :value="form.selectedGood && form.selectedGood.gbsGoodClass ? form.selectedGood.gbsGoodClass.gcName : ''"
          class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="单位" prop="reportPCode">
        <el-input
          :value="form.selectedGood && form.selectedGood.gbsGoodUnit ? form.selectedGood.gbsGoodUnit.guName : ''"
          class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="工单批次" prop="reportOBatch">
        <el-input v-model="form.reportOBatch"
                  class="my-input" disabled
        />
      </el-form-item>

      <el-divider>
        <div class="my-divider">任务信息</div>
      </el-divider>
      <el-form-item label="任务名称" prop="reportTCodeName">
        <MyPopoverBtn placeholder="请选择工单任务"
                      :content="form.selectedOrderTask ?  form.selectedOrderTask.wtaskName : ''"
                      @click="openOrderTask"
                      class="my-input"
                      :disabled="form.reportQcCode"
        />
      </el-form-item>
      <el-form-item label="任务编号" prop="reportTCode">
        <el-input v-model="form.reportTCode"
                  class="my-input" disabled
        />
      </el-form-item>
      <el-form-item label="生产单元" prop="reportWCellName">
        <el-input v-model="form.reportWCellName" disabled
                  class="my-input"
        />
      </el-form-item>
      <el-form-item label="工艺路线" prop="reportRouteName">
        <el-input v-model="form.reportRouteName" disabled
                  class="my-input"
        />
      </el-form-item>
      <el-form-item label="工序编号" prop="reportProcedure">
        <el-input v-model="form.reportProcedure" disabled
                  class="my-input"
        />
      </el-form-item>
      <el-form-item label="工序名称" prop="reportProcedureName">
        <el-input v-model="form.reportProcedureName" disabled
                  class="my-input"
        />
      </el-form-item>
      <el-form-item label="生产班组" prop="reportGroupName">
        <el-input v-model="form.reportGroupName" disabled
                  class="my-input"
        />
      </el-form-item>
      <el-form-item label="任务负责人" prop="wtaskManager">
        <el-input :value="form.selectedOrderTask ? form.selectedOrderTask.wtaskManager : ''" disabled
                  class="my-input"
        />
      </el-form-item>

      <el-divider>
        <div class="my-divider">检验项目</div>
      </el-divider>
      <div style="display: block;flex: 1">
        <div>
          <el-button type="primary" @click="handleToAddProject">添加</el-button>
          <el-button type="primary" plain @click="handleRemoveProject">
            移除
          </el-button>
        </div>
        <el-table :data="form.selectedProjectList ? form.selectedProjectList: []"
                  @selection-change="selectChangeProject"
        >
          <el-table-column type="selection" align="center" />
          <el-table-column label="序号" type="index" align="center" />
          <el-table-column label="质检项目编码" align="center" prop="qualityCode" />
          <el-table-column label="项目名称" align="center" prop="qualityProjectName" />
          <el-table-column label="检验标准" align="center" prop="qualityProjectStandard" />
          <el-table-column label="项目最大值" align="center" prop="qualityProjectMax" />
          <el-table-column label="项目最小值" align="center" prop="qualityProjectMin" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                v-hasPermi="['qc:project:query']"
                @click="lookDrawbackDialog(scope.$index)"
              >查看缺陷
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                v-hasPermi="['qc:project:edit']"
                @click="openDrawbackDialog(scope.$index)"
              >缺陷填报
              </el-button>
            </template>
          </el-table-column>
        </el-table>

      </div>

      <el-divider>
        <div class="my-divider">检验结果</div>
      </el-divider>
      <el-form-item label="检测数量" prop="reportQcNum">
        <el-input-number v-model.number="form.reportQcNum" :min="1"
                         class="my-input"
        />
      </el-form-item>
      <el-form-item label="合格数量" prop="reportGood">
        <el-input-number v-model.number="form.reportGood" :min="0"
                         class="my-input"
        />
      </el-form-item>
      <el-form-item label="检测结果" prop="reportResult">
        <el-select
          v-model="form.reportResult"
          placeholder="请选择检验类型"
          class="my-input"
        >
          <el-option
            v-for="dict in getQualityYesNo"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>

    <!--    提交按钮-->
    <MyConfirmBtnGroup :after-cancel-open-page="backPage" :ok-event="submitForm" />

    <!--    选择检验人 -->
    <el-dialog title="选择检验人" :visible.sync="qcPeoDialogVisible" width="50%" append-to-body>
      <RuoYiUserTree :selected-user.sync="form.selectedQcPeo" @close="closeQcPeo" />
    </el-dialog>

    <!--    选择工单 -->
    <el-dialog title="选择工单" :visible.sync="workOrderDialogVisible" width="50%" append-to-body>
      <div class="my-dialog-query-box">
        <el-form :model="workOrderQueryParams" ref="workOrderQueryForm" size="small" :inline="true"
        >
          <el-form-item prop="workOrderName">
            <el-input
              v-model="workOrderQueryParams.workOrderName"
              placeholder="请输入工单名称"
              clearable
              @keyup.enter.native="workOrderHandleQuery"
            />
          </el-form-item>
          <el-form-item prop="workOrderProduct">
            <el-input
              v-model="workOrderQueryParams.workOrderProduct"
              placeholder="请输入产品名称"
              clearable
              @keyup.enter.native="workOrderHandleQuery"
            />
          </el-form-item>
          <el-form-item prop="workOrderProduct">
            <el-input
              v-model="workOrderQueryParams.workOrderProduct"
              placeholder="请输入产品编号"
              clearable
              @keyup.enter.native="workOrderHandleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="workOrderHandleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="workOrderResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>


      <el-table v-loading="workOrderLoading" :data="workOrderList" highlight-current-row
                @current-change="handleWorkOrderChange"
      >
        <el-table-column
          align="center"
          label="序号"
        >
          <template slot-scope="scope">
            <div style="display: inline-block; margin-right: 20px">
              <MyRadio v-model="preSelectedWorkOrder.workOrderCode"
                       :label="scope.row.workOrderCode"
                       radio-name="selectedWorkOrder"
              />
            </div>
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="工单编号" align="center" prop="workOrderCode" />
        <el-table-column label="工单名称" align="center" prop="workOrderName" />
        <el-table-column label="产品名称" align="center" prop="gbsGood.pdName" />
        <el-table-column label="产品编号" align="center" prop="gbsGood.pdCode" />
        <el-table-column label="规格型号" align="center" prop="gbsGood.pdSpecType" />
        <el-table-column label="产品类型" align="center" prop="gbsGood.gbsGoodClass.gcName" />
        <el-table-column label="单位" align="center" prop="gbsGood.gbsGoodUnit.guName" />
      </el-table>

      <pagination
        v-show="workOrderTotal>0"
        :total="workOrderTotal"
        :page.sync="workOrderQueryParams.pageNum"
        :limit.sync="workOrderQueryParams.pageSize"
        @pagination="workOrderGetList"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okWorkOrder">确 定</el-button>
        <el-button @click="cancelWorkOrder">取 消</el-button>
      </div>
    </el-dialog>

    <!--    选择质检项目-->
    <el-dialog title="选择质检项目" :visible.sync="projectDialogVisible" width="50%" append-to-body>
      <el-form :model="projectQueryParams" ref="projectQueryForm" size="small" :inline="true" label-width="68px">
        <el-form-item prop="qualityCode">
          <el-input
            v-model="projectQueryParams.qualityCode"
            placeholder="请输入质检项目编码"
            clearable
            @keyup.enter.native="projectHandleQuery"
          />
        </el-form-item>
        <el-form-item prop="qualityProjectName">
          <el-input
            v-model="projectQueryParams.qualityProjectName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="projectHandleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="projectHandleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="projectResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="projectLoading" :data="projectList"
                @selection-change="selectChangePreProject"
      >
        <el-table-column type="selection" align="center" />
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column label="质检项目编码" align="center" prop="qualityCode" />
        <el-table-column label="项目名称" align="center" prop="qualityProjectName" />
        <el-table-column label="检验标准" align="center" prop="qualityProjectStandard" />
        <el-table-column label="项目最大值" align="center" prop="qualityProjectMax" />
        <el-table-column label="项目最小值" align="center" prop="qualityProjectMin" />
      </el-table>

      <pagination
        v-show="projectTotal>0"
        :total="projectTotal"
        :page.sync="projectQueryParams.pageNum"
        :limit.sync="projectQueryParams.pageSize"
        @pagination="projectGetList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okProject">确 定</el-button>
        <el-button @click="cancelProject">取 消</el-button>
      </div>
    </el-dialog>

    <!--    选择工单任务 -->
    <el-dialog title="选择工单任务" :visible.sync="orderTaskDialogVisible" width="50%" append-to-body
               @open="openingOrderTask"
    >
      <div class="my-dialog-query-box">
        <el-form :model="orderTaskQueryParams" ref="orderTaskQueryForm" size="small" :inline="true"
                 label-width="68px"
        >
          <el-form-item prop="wtaskWoCode">
            <el-input
              v-model="orderTaskQueryParams.wtaskWoCode"
              placeholder="请输入工单任务编号"
              clearable
              @keyup.enter.native="orderTaskHandleQuery"
            />
          </el-form-item>
          <el-form-item prop="wtaskProcedureCode">
            <el-input
              v-model="orderTaskQueryParams.wtaskProcedureCode"
              placeholder="请输入工序code"
              clearable
              @keyup.enter.native="orderTaskHandleQuery"
            />
          </el-form-item>
          <el-form-item prop="wtaskManager">
            <el-input
              v-model="orderTaskQueryParams.wtaskQcPeo"
              placeholder="请输入任务负责人"
              clearable
              @keyup.enter.native="orderTaskHandleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="orderTaskHandleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="orderTaskResetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table v-loading="orderTaskLoading" :data="workTaskList" highlight-current-row
                @current-change="handleOrderTaskChange"
      >
        <el-table-column
          align="center"
          label="序号"
        >
          <template slot-scope="scope">
            <div style="display: inline-block; margin-right: 20px">
              <MyRadio v-model="preSelectedOrderTask.wtaskCode"
                       :label="scope.row.wtaskCode"
                       radio-name="selectedWtaskCode"
              />
            </div>
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="任务编号" align="center" prop="wtaskCode" />
        <el-table-column label="任务名称" align="center" prop="wtaskName" />
        <el-table-column label="生产单元" align="center" prop="pbsWorkGroup.pbsWorkCell.workCellName" />
        <el-table-column label="工艺路线" align="center" prop="pbsProcedureRoute.prouteName" />
        <el-table-column label="工序名称" align="center" prop="pbsProcedure.procedureName" />
        <el-table-column label="工序编号" align="center" prop="wtaskProcedureCode" />
        <el-table-column label="任务负责人" align="center" prop="wtaskManager" />
      </el-table>

      <pagination
        v-show="orderTaskTotal>0"
        :total="orderTaskTotal"
        :page.sync="orderTaskQueryParams.pageNum"
        :limit.sync="orderTaskQueryParams.pageSize"
        @pagination="orderTaskGetList"
      />

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okOrderTask">确 定</el-button>
        <el-button @click="cancelOrderTask">取 消</el-button>
      </div>
    </el-dialog>

    <!--    选择缺陷-->
    <el-dialog title="选择缺陷" :visible.sync="drawbackDialogVisible" width="50%" append-to-body>
      <el-form :model="drawbackQueryParams" ref="drawbackQueryForm" size="small" :inline="true" label-width="68px">
        <el-form-item prop="drawbackCode">
          <el-input
            v-model="drawbackQueryParams.drawbackCode"
            placeholder="请输入缺陷编号"
            clearable
            @keyup.enter.native="drawbackHandleQuery"
          />
        </el-form-item>
        <el-form-item prop="drawbackName">
          <el-input
            v-model="drawbackQueryParams.drawbackName"
            placeholder="请输入缺陷名称"
            clearable
            @keyup.enter.native="drawbackHandleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="drawbackHandleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="drawbackResetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="drawbackTable" v-loading="drawbackLoading" :data="drawbackList"
                @selection-change="handleSelectDrawbackChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="缺陷编号" align="center" prop="drawbackCode" />
        <el-table-column label="缺陷名称" align="center" prop="drawbackName" />
        <el-table-column label="缺陷类型编码" align="center" prop="qcDrawbackType.drawbackTypeName" />
        <el-table-column label="缺陷状态" align="center" prop="drawbackStatus">
          <template slot-scope="scope">
            <dict-tag :options="getStatus" :value="scope.row.drawbackStatus" />
          </template>
        </el-table-column>
        <el-table-column label="缺陷描述" align="center" prop="drawbackDetail" />
      </el-table>

      <pagination
        v-show="drawbackTotal>0"
        :total="drawbackTotal"
        :page.sync="drawbackQueryParams.pageNum"
        :limit.sync="drawbackQueryParams.pageSize"
        @pagination="drawbackGetList"
      />


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okDrawback">确 定</el-button>
        <el-button @click="cancelDrawback">取 消</el-button>
      </div>
    </el-dialog>

    <!--    查看缺陷-->
    <el-dialog title="选择缺陷" :visible.sync="projectDrawbackDialogVisible" width="50%" append-to-body>
      <el-table ref="projectDrawbackTable" :data="projectDrawbackList">
        <el-table-column label="缺陷编号" align="center" prop="drawbackCode" />
        <el-table-column label="缺陷名称" align="center" prop="drawbackName" />
        <el-table-column label="缺陷类型编码" align="center" prop="qcDrawbackType.drawbackTypeName" />
        <el-table-column label="缺陷状态" align="center" prop="drawbackStatus">
          <template slot-scope="scope">
            <dict-tag :options="getStatus" :value="scope.row.drawbackStatus" />
          </template>
        </el-table-column>
        <el-table-column label="缺陷描述" align="center" prop="drawbackDetail" />
      </el-table>

      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDrawback">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addProQuality, getProQualityByQcCode, updateProQuality } from '@/api/yyj/qc/proQuality'
import MyPopoverBtn from '@/components/MyPopoverBtn/index.vue'
import MyConfirmBtnGroup from '@/components/MyConfirmBtnGroup/index.vue'
import { listWorkOrder } from '@/api/yyj/pro/workOrder'
import { listWorkTask } from '@/api/yyj/pro/workTask'
import { listProject } from '@/api/yyj/qc/project'
import { listDrawback } from '@/api/yyj/qc/drawback'
import MyRadio from '@/components/MyRadio/index.vue'
import RuoYiUserTree from '@/components/RuoYiUserTree/index.vue'

export default {
  name: 'proQualityChange',
  components: { RuoYiUserTree, MyRadio, MyConfirmBtnGroup, MyPopoverBtn },
  dicts: ['qc_quality_type', 'yyj_common_status', 'qc_quality_yes_no'],
  data() {
    return {
      backPage: '/qc/proQuality',
      qcPeoDialogVisible: false,
      workOrderDialogVisible: false,
      projectDialogVisible: false,
      orderTaskDialogVisible: false,
      drawbackDialogVisible: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reportQcName: [
          { required: true, message: '检验单名称不能为空', trigger: 'blur' }
        ],
        reportType: [
          { required: true, message: '检验类型不能为空', trigger: 'change' }
        ],
        reportQcPeo: [
          { required: true, message: '检验人不能为空' }
        ]
      },
      // 遮罩层
      workOrderLoading: true,
      // 总条数
      workOrderTotal: 0,
      // 生产工单表格数据
      workOrderList: [],
      // work-order dialog
      workOrderQueryParams: {
        pageNum: 1,
        pageSize: 10,
        scheduleCode: null,
        workOrderName: null,
        workOrderDesc: null,
        workOrderStatus: null,
        workOrderCloseR: null,
        workOrderProduct: null,
        workOrderRoute: null,
        workOrderBatch: null,
        workOrderNum: null
      },
      // 预选的工单
      preSelectedWorkOrder: {},
      // 查询参数
      orderTaskQueryParams: {
        pageNum: 1,
        pageSize: 10,
        wtaskQcPeo: null,
        wtaskWoCode: null,
        wtaskProcedureCode: null,
        wtaskStatus: null,
        wtaskPCloseReason: null,
        wtaskPDesc: null
      },
      // 遮罩层
      orderTaskLoading: true,
      // 总条数
      orderTaskTotal: 0,
      // 工单任务表格数据
      workTaskList: [],
      // 预选的工单任务
      preSelectedOrderTask: {},
      // 质检项目加载
      projectLoading: false,
      preSelectedProjectList: [],
      // 准备移除的项目
      preRemoveProjectList: [],
      // dialog - project
      projectList: [],
      // 总条数
      projectTotal: 0,
      // 查询参数
      projectQueryParams: {
        pageNum: 1,
        pageSize: 10,
        qualityCode: null,
        qualityProjectName: null,
        qualityProjectStandard: null,
        qualityProjectMax: null,
        qualityProjectMin: null,
        qualityProjectDesc: null
      },
      // 查询参数
      drawbackQueryParams: {
        pageNum: 1,
        pageSize: 10,
        drawbackCode: null,
        drawbackTypeCode: null,
        drawbackTypeName: null,
        drawbackName: null,
        drawbackStatus: null,
        drawbackDetail: null,
        drawbackDesc: null
      },
      drawbackLoading: false,
      drawbackTotal: 0,
      drawbackList: [],
      preSelectedDrawbackList: [],
      currentDrawbackIndex: -1,
      projectDrawbackDialogVisible: false,
      projectDrawbackList: []
    }
  },
  created() {
    const reportQcCode = this.$route.params.reportQcCode
    if (reportQcCode) {
      Promise.all([
        this.workOrderGetList(),
        this.projectGetList(),
        this.drawbackGetList(),
        this.getDetailInfo(reportQcCode)
      ])
    } else {
      Promise.all([
        this.workOrderGetList(),
        this.projectGetList(),
        this.drawbackGetList()
      ])
    }
  },
  computed: {
    getQualityType() {
      return this.dict.type.qc_quality_type
    },
    getStatus() {
      return this.dict.type.yyj_common_status
    },
    getQualityYesNo() {
      return this.dict.type.qc_quality_yes_no
    }
  },
  methods: {
    lookDrawbackDialog(index) {
      this.projectDrawbackList = this.form.selectedProjectList[index].drawbackList
      this.projectDrawbackDialogVisible = true
    },
    closeDrawback() {
      this.projectDrawbackList = []
      this.projectDrawbackDialogVisible = false
    },
    // 获取此页面的详细信息
    async getDetailInfo(reportQcCode) {
      const res = await getProQualityByQcCode(reportQcCode)
      console.log(' res ', res)
      this.form = res.data
      this.form.selectedGood = res.selectedGood
      this.form.selectedOrderTask = res.selectedOrderTask
      this.form.selectedQcPeo = res.selectedQcPeo
      this.form.selectedProjectList = res.selectedProjectList
    },
    // 打开工单任务
    openOrderTask() {
      if (!this.form.reportOCode) {
        this.$modal.msgError('请先选择工单!')
        return
      }
      this.orderTaskDialogVisible = true
    },
    // 打开工单任务前
    openingOrderTask() {
      this.orderTaskGetList()
    },
    // 打开drawback-dialog
    openDrawbackDialog(index) {
      this.currentDrawbackIndex = index
      this.drawbackDialogVisible = true
    },
    okDrawback() {
      this.form.selectedProjectList[this.currentDrawbackIndex].drawbackList = this.preSelectedDrawbackList
      this.cancelDrawback()
    },
    cancelDrawback() {
      this.currentDrawbackIndex = -1
      this.drawbackDialogVisible = false
    },
    // 预选择drawback
    handleSelectDrawbackChange(selection) {
      this.preSelectedDrawbackList = selection
    },
    /** 搜索按钮操作 */
    drawbackHandleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    drawbackResetQuery() {
      this.resetForm('drawbackQueryForm')
      this.drawbackHandleQuery()
    },
    /** 查询缺陷列表 */
    async drawbackGetList() {
      this.drawbackLoading = true
      const res = await listDrawback(this.drawbackQueryParams)
      this.drawbackList = res.rows
      this.drawbackTotal = res.total
      this.drawbackLoading = false
    },
    // 预选择project
    selectChangePreProject(selection) {
      this.preSelectedProjectList = selection
    },
    // 确定项目
    okProject() {
      this.form.selectedProjectList = this.preSelectedProjectList
      this.cancelProject()
    },
    // 取消项目
    cancelProject() {
      this.projectDialogVisible = false
    },
    /** 搜索按钮操作 */
    projectHandleQuery() {
      this.projectQueryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    projectResetQuery() {
      this.resetForm('projectQueryForm')
      this.projectHandleQuery()
    },
    // 选择项目
    selectChangeProject(selection) {
      this.preRemoveProjectList = selection
    },
    // 去添加项目
    handleToAddProject() {
      this.projectDialogVisible = true
    },
    // 移除已选择的项目
    handleRemoveProject(index) {
      this.form.selectedProjectList.splice(index, 1)
    },
    /** 查询质检项目列表 */
    async projectGetList() {
      this.projectLoading = true
      const res = await listProject(this.projectQueryParams)
      this.projectList = res.rows
      this.projectTotal = res.total
      this.projectLoading = false
    },
    // 确认工单任务
    okOrderTask() {
      console.log('preSelectedOrderTask ', this.preSelectedOrderTask)
      // this.form.reportQcPeo = this.form.selectedWorkOrder.userId
      this.form.selectedOrderTask = this.preSelectedOrderTask
      this.form.reportTCode = this.form.selectedOrderTask.wtaskCode
      this.form.reportTCodeName = this.form.selectedOrderTask.wtaskName
      this.form.reportWCellName = this.form.selectedOrderTask.workCellName
      this.form.reportRouteName = this.form.selectedOrderTask.prouteName
      this.form.reportProcedureName = this.form.selectedOrderTask.procedureName
      this.form.reportProcedure = this.form.selectedOrderTask.wtaskProcedureCode
      this.form.reportGroupName = this.form.selectedOrderTask.wtaskGroupName
      this.cancelOrderTask()
    },
    // 取消工单任务
    cancelOrderTask() {
      this.preSelectedOrderTask = {}
      this.orderTaskDialogVisible = false
    },
    // 选择工单任务
    handleOrderTaskChange(val) {
      this.preSelectedOrderTask = val
    },
    /** 查询工单任务列表 */
    async orderTaskGetList() {
      this.orderTaskLoading = true
      this.orderTaskQueryParams.wtaskQcPeo = this.form.reportQcPeo
      this.orderTaskQueryParams.wtaskWoCode = this.form.reportOCode
      const res = await listWorkTask(this.orderTaskQueryParams)
      this.workTaskList = res.rows
      this.orderTaskTotal = res.total
      this.orderTaskLoading = false
    },
    /** 搜索按钮操作 */
    orderTaskHandleQuery() {
      this.orderTaskQueryParams.pageNum = 1
      this.orderTaskGetList()
    },
    /** 重置按钮操作 */
    orderTaskResetQuery() {
      this.resetForm('orderTaskQueryForm')
      this.orderTaskHandleQuery()
    },
    // 确认工单
    okWorkOrder() {
      console.log('preSelectedWorkOrder ', this.preSelectedWorkOrder)
      this.form.selectedWorkOrder = { ...this.preSelectedWorkOrder }
      this.form.selectedGood = this.form.selectedWorkOrder.gbsGood
      this.form.reportOCode = this.form.selectedWorkOrder.workOrderCode
      this.form.reportOCodeName = this.form.selectedWorkOrder.workOrderName
      this.form.reportOBatch = this.form.selectedWorkOrder.workOrderBatch
      this.form.reportPCode = this.form.selectedGood.pdCode
      // this.form.reportQcPeo = this.form.selectedWorkOrder.userId
      this.cancelWorkOrder()
    },
    // 取消工单
    cancelWorkOrder() {
      this.preSelectedWorkOrder = {}
      this.workOrderDialogVisible = false
    },
    // 选择工单
    handleWorkOrderChange(val) {
      this.preSelectedWorkOrder = val
    },
    /** 查询生产工单列表 */
    async workOrderGetList() {
      this.workOrderLoading = true
      const res = await listWorkOrder(this.workOrderQueryParams)
      this.workOrderList = res.rows
      this.workOrderTotal = res.total
      this.workOrderLoading = false
    },
    // 重置工单dialog
    workOrderResetQuery() {
      this.resetForm('workOrderQueryForm')
      this.workOrderHandleQuery()
    },
    // 搜索dialog
    workOrderHandleQuery() {
      this.workOrderQueryParams.pageNum = 1
      this.workOrderGetList()
    },
    // 取消检验人
    closeQcPeo() {
      if (this.form.selectedQcPeo) {
        this.form.reportQcPeo = this.form.selectedQcPeo.userId
        this.form.reportQcPeoName = this.form.selectedQcPeo.userName
      }
      this.qcPeoDialogVisible = false
    },
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          this.$modal.loading('正在提交...')
          if (this.form.reportCode != null) {
            await updateProQuality(this.form)
            this.$modal.msgSuccess('修改成功')
          } else {
            await addProQuality(this.form)
            this.$modal.msgSuccess('新增成功')
          }
          this.afterSubmit()
        }
      })
    },
    afterSubmit() {
      this.$tab.closeOpenPage()
      this.reset()
      this.$tab.refreshPage({
        path: this.backPage
      })
      this.$modal.closeLoading()
    }
  }
}
</script>

<style scoped lang="scss">
@import '@/assets/styles/yyj/change';

.el-dialog .el-form-item {
  width: auto;
  padding: 0 10px 0 0 !important;
}
</style>
